package org.hamster.multidropbox.mainView.view.file
{
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	
	import mx.collections.ArrayCollection;
	import mx.controls.CheckBox;
	import mx.managers.PopUpManager;
	
	import org.hamster.common.puremvc.mediator.BaseMediator;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.main.utils.AppUtil;
	import org.hamster.multidropbox.main.view.AppPopUp;
	import org.hamster.multidropbox.mainView.event.MainViewEvent;
	
	public class LinkFolderUploadPanelMediator extends BaseMediator
	{
		public static const NAME:String = "org.hamster.multidropbox.mainView.view.file.LinkFolderUploadPanelMediator";
		
		public static const STATUS_NOTHING:uint         = 0;
		public static const STATUS_CLIENT_SELECTED:uint = 0x001;
		public static const STATUS_FILE_SELECTED:uint   = 0x002;
		
		private var folderName:String;
		
		
		public function LinkFolderUploadPanelMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			
		}
		
		override public function onRemove():void
		{
			
		}
		
		override protected function statusChanged():void
		{
			super.statusChanged();
			if (isStatusAvailable(STATUS_CLIENT_SELECTED) 
				&& isStatusAvailable(STATUS_FILE_SELECTED)) {
				this.app.uploadBtn.enabled = true;
			} else {
				this.app.uploadBtn.enabled = false;
			}
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				MainViewEvent.LINK_FOLDER_UPLOAD_POPUP_SHOW,
				MainViewEvent.LINK_FOLDER_UPLOAD_POPUP_HIDE
			];
		}
		
		public function handleLinkFolderUploadPopupShow(data:Object):void
		{
			viewComponent = AppUtil.popup(LinkFolderUploadPanel);
			viewStatus = STATUS_FILE_SELECTED | STATUS_CLIENT_SELECTED;
			app.addEventListener(MainViewEvent.DATA_RENDERER_ITEM_CLICK, checkBoxItemClickHandler);
			app.uploadBtn.addEventListener(MouseEvent.CLICK, uploadBtnClickHandler);
			var clientList:ArrayCollection = data.clientList;
			this.app.clientList = clientList;
			var fileList:ArrayCollection =data.fileList;
			this.app.successFileList = fileList;
			this.folderName = data.folderName;
		}
		
		public function handleLinkFolderUploadPopupHide(data:Object):void
		{
			PopUpManager.removePopUp(app);
			
			app.removeEventListener(MainViewEvent.DATA_RENDERER_ITEM_CLICK, checkBoxItemClickHandler);
			app.uploadBtn.removeEventListener(MouseEvent.CLICK, uploadBtnClickHandler);
		}
		
		private function checkBoxItemClickHandler(event:MainViewEvent):void
		{
			for (var i:int = 0; i < app.clientListDataGroup.numElements; i++) {
				var checkboxSelected:Boolean = (app.clientListDataGroup.getElementAt(i) as Object).selected;
				if (checkboxSelected) {
					this.setStatusAvailable(STATUS_CLIENT_SELECTED, true);
					return;
				}
			}
			
			this.setStatusAvailable(STATUS_CLIENT_SELECTED, false);
		}
		
		private function uploadBtnClickHandler(event:MouseEvent):void
		{
			var selectedClientList:ArrayCollection = new ArrayCollection();
			for (var i:int = 0; i < app.clientListDataGroup.numElements; i++) {
				var checkboxSelected:Boolean = (app.clientListDataGroup.getElementAt(i) as Object).selected;
				if (checkboxSelected) {
					selectedClientList.addItem(this.app.clientList.getItemAt(i));
				}
			}
			var body:Object = {
				folderName : folderName,
				clientList : selectedClientList,
				fileRefList : app.successFileList
			}
			this.sendNotification(AppDropboxEvent.DROPBOX_LINK_FOLDERS_UPLOAD_REQUEST, body);
		}
		
		
		public function get app():LinkFolderUploadPanel
		{
			return viewComponent as LinkFolderUploadPanel;
		}
	}
}